package com.ywj.springboot_01_quickstart.controller;

import com.ywj.springboot_01_quickstart.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
//@Controller
public class RequestController {

    //原始方式
//    @RequestMapping("/simpleParam")
//    public String simpleParam(HttpServletRequest request) {
//        String name = request.getParameter("name");
//        String ageStr = request.getParameter("age");
//        int age = Integer.parseInt(ageStr);
//
//        System.out.println(name + "  :  " + age);
//        return "success";
//    }


    //springboot方式   形参名和请求参数名保持一致
//    @RequestMapping("/simpleParam")
//    public String simpleParam(String name, Integer age) {
//        System.out.println(name + "  :  " + age);
//        return "success";
//    }

    //springboot方式   请求参数名和形参名不相同
//    @RequestMapping("/simpleParam")
//    public String simpleParam(String username, Integer age) {
//        System.out.println(username + "  :  " + age);
//        return "success";
//    }

    //springboot方式   请求参数名和形参名不相同，加上@RequestParam注解
    @RequestMapping("/simpleParam")
    public String simpleParam(@RequestParam("name") String username, Integer age) {
        System.out.println(username + "  :  " + age);
        return "success";
    }

    //springboot方式   请求参数名和形参名不相同，加上@RequestParam注解，参数可选
//    @RequestMapping("/simpleParam")
//    public String simpleParam(@RequestParam(value = "name", required = false) String username, Integer age) {
//        System.out.println(username + "  :  " + age);
//        return "success";
//    }


    //实体参数：简单实体对象
    @RequestMapping("/simplePojo")
    public String simplePojo(User user) {
        System.out.println(user);
        return "success";
    }

    //实体参数：复杂实体对象
    @RequestMapping("/complexPojo")
    public String complexPojo(User user) {
        System.out.println(user);
        return "success";
    }


    //数组集合参数
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] course) {
        System.out.println(Arrays.toString(course));
        return "success";
    }

    //数组集合参数
    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> course) {
        System.out.println(course);
        return "success";
    }


    //JSON参数
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user) {
        System.out.println(user);
        return "success";
    }



    //日期时间参数
    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime) {
        System.out.println(updateTime);
        return "success";
    }




    //路径参数  单个路径参数
    @RequestMapping("/path/{id}")
    public String pathParam(@PathVariable Integer id) {
        System.out.println(id);
        return "success";
    }



    //路径参数  多个路径参数
    @RequestMapping("/path/{id}/{name}")
    public String pathParam2(@PathVariable Integer id, @PathVariable String name) {
        System.out.println(id + " : " + name);
        return "success";
    }















}
